package me.send.produce.data.controller;

import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import me.send.business.base.Pager;
import me.send.common.poi.ExcelUtil;
import me.send.common.poi.ExcelUtil.ExcelExportData;
import me.send.data.service.pojo.PayOrder;
import me.send.data.service.pojo.base.DatatablesViewPage;
import me.send.data.service.service.PayOrderService;
import me.send.data.service.service.TreeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "user")
public class PayOrderController {

	@Autowired
	private PayOrderService payOrderService;

	@Autowired
	private TreeService treeService;

	 @RequestMapping(value = "payOrder/excel/export")    
	    public void exportExcel(HttpServletRequest request, HttpServletResponse response)     
	    throws Exception { 
	//获取前台额外传递过来的查询条件  
	        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
	        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String start_time = request.getParameter("start_time"); 
	        String end_time = request.getParameter("end_time");  
	        String phone = request.getParameter("phone");  
	        String status = request.getParameter("status");  
	        String payWay = request.getParameter("payWay");  
	        String payType = request.getParameter("payType");  
	        Integer paramPayWay=null;
	        Integer paramPayType=null;
	        Integer paramStatus=null;
	        if(payWay==null||(payWay!=null&&"".equals(payWay.trim()))){
	        	payWay=null;
	        }else{
	        	paramPayWay=Integer.parseInt(payWay);
	        }
	        if(payType==null||(payType!=null&&"".equals(payType.trim()))){
	        	payType=null;
	        }else{
	        	paramPayType=Integer.parseInt(payType);
	        }
	        if(status==null||(status!=null&&"".equals(status.trim()))){
	        	status=null;
	        }else {
	        	paramStatus=Integer.parseInt(status);
	        }
	        if(phone==null||(phone!=null&&"".equals(phone.trim()))){
	        	phone=null;
	        }
	        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
	        	Date startDate=null;
	        	Date endDate=null;
	        	try {
	        		startDate=sdf.parse(start_time);
	        		start_time=format.format(startDate);
	        		endDate=sdf.parse(end_time);
	        		end_time=format.format(endDate);
	        	} catch (ParseException e) {
	        		// TODO Auto-generated catch block
	        		e.printStackTrace();
	        	}
	        }else{
	        	start_time=null;
	        	end_time=null;
	        }
	        
	        Integer count=payOrderService.count(phone,paramStatus,start_time,end_time,paramPayWay,paramPayType);
	        Pager page=new Pager();
	        page.setRows(count);
	        Integer p=(0/count)+1;
	        page.setPage(p);
	        
	                //随便组织的查询结果  
	        List<PayOrder> list=payOrderService.list(phone,paramStatus,start_time,end_time,paramPayWay,paramPayType,page); 
		 
		 	List<String[]> columNames = new ArrayList<String[]>();
	        columNames.add(new String[] { "支付订单号", "雇主手机号", "支付金额", "创建时间", "完成时间", "状态（0：未支付，1：已支付）", "支付类型（0：支付宝，1：微信）", "支付方式（0：充值，1：支付）","呼单ID（充值时不是呼单ID）" });

	        List<String[]> fieldNames = new ArrayList<String[]>();
	        fieldNames.add(new String[] { "payOrderNo", "telephone", "amount", "createTime", "finishTime", "status", "payType", "payWay","orderId" });

	        LinkedHashMap<String, List<?>> map = new LinkedHashMap<String, List<?>>();
	        map.put("支付订单", list);
	        
	        
	        ExcelExportData setInfo = new ExcelExportData();
	        setInfo.setDataMap(map);
	        setInfo.setFieldNames(fieldNames);
	        setInfo.setTitles(new String[] { "支付订单报表"});
	        setInfo.setColumnNames(columNames);
		 
		 
	        response.setContentType("application/vnd.ms-excel");    
	        response.setHeader("Content-disposition", "attachment;filename=pay_order.xls");    
	        OutputStream ouputStream = response.getOutputStream();    
	        ouputStream.write(ExcelUtil.export2ByteArray(setInfo));
	        ouputStream.flush();    
	        ouputStream.close();    
	   }    

	// 查询提现订单列表
	@RequestMapping(value = "user/pay/list")
	public ModelAndView withdraw_list(HttpServletRequest request) {
		ModelAndView modelAndView = new ModelAndView(
				"stage/user/pay_order");
		modelAndView.addObject("LoadTree",
				treeService.main_tree(0, request, "user/user/pay/list"));
		modelAndView.addObject("module", "雇主支付订单列表");
		return modelAndView;
	}

	
	@RequestMapping(value="pay/list", method=RequestMethod.GET)  
    @ResponseBody  
    public DatatablesViewPage<PayOrder> datatablesTest(HttpServletRequest request){  
//获取分页控件的信息  
        String start = request.getParameter("start");  
        String length = request.getParameter("length"); 
        System.out.println(start);
        System.out.println(length);
//获取前台额外传递过来的查询条件  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy/MM/dd HH:mm");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String start_time = request.getParameter("start_time"); 
        String end_time = request.getParameter("end_time");  
        String phone = request.getParameter("phone");  
        String status = request.getParameter("status");  
        String payWay = request.getParameter("payWay");  
        String payType = request.getParameter("payType");  
        Integer paramPayWay=null;
        Integer paramPayType=null;
        Integer paramStatus=null;
        if(payWay==null||(payWay!=null&&"".equals(payWay.trim()))){
        	payWay=null;
        }else{
        	paramPayWay=Integer.parseInt(payWay);
        }
        if(payType==null||(payType!=null&&"".equals(payType.trim()))){
        	payType=null;
        }else{
        	paramPayType=Integer.parseInt(payType);
        }
        if(status==null||(status!=null&&"".equals(status.trim()))){
        	status=null;
        }else {
        	paramStatus=Integer.parseInt(status);
        }
        if(phone==null||(phone!=null&&"".equals(phone.trim()))){
        	phone=null;
        }
        if(start_time!=null&&!"".equals(start_time.trim())&&end_time!=null&&!"".equals(end_time.trim())){
        	Date startDate=null;
        	Date endDate=null;
        	try {
        		startDate=sdf.parse(start_time);
        		start_time=format.format(startDate);
        		endDate=sdf.parse(end_time);
        		end_time=format.format(endDate);
        	} catch (ParseException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}
        }else{
        	start_time=null;
        	end_time=null;
        }
        
        Pager page=new Pager();
        Integer row=Integer.parseInt(length);
        page.setRows(row);
        Integer startInt=Integer.parseInt(start);
        Integer p=(startInt/row)+1;
        page.setPage(p);
        
                //随便组织的查询结果  
        List<PayOrder> list=payOrderService.list(phone,paramStatus,start_time,end_time,paramPayWay,paramPayType,page); 
        Integer count=payOrderService.count(phone,paramStatus,start_time,end_time,paramPayWay,paramPayType);
        DatatablesViewPage<PayOrder> view = new DatatablesViewPage<PayOrder>();  
        view.setiTotalDisplayRecords(count);  
        view.setiTotalRecords(count);  
          
        view.setAaData(list);  
        return view;  
    }  
	
}





